import { ref,reactive } from 'vue'
import { getUserDataAPI ,rechargeAPI} from '@/api/user'
import storage from "@/utils/storage";
export function useWalletData() {
    const userInfo = ref(null)

    const popupRef = ref(null)
    const chargeData = reactive({
        balance: 10,
        password: ''
    })
    function getUserData () {
        const currentUser  = storage.get('userInfo')
        getUserDataAPI(currentUser.username).then(res => {
            userInfo.value = res
        })
    }

    function chargeWallet () {
        if(!chargeData.password) {
            uni.showToast({ title: '请输入密码', icon: 'none'})
        }
        uni.showLoading({ title: '充值中...'})
        rechargeAPI(chargeData).then(res => {
            uni.showToast({title: '充值成功', icon: 'success'})
            uni.hideLoading()
        }).finally(_ => {
            uni.hideLoading()
            popupRef.value.close()
        })
    }
    return {
        getUserData,
        userInfo,
        popupRef,
        chargeData,
        chargeWallet
    }
}
